requirejs.config({
    baseUrl : "./js/",
    urlArgs : "va=" + (new Date()).getTime(),
    //paths 只能引入模块 使用了 define
    paths : {
        "jquery" : ["lib/jquery-1.11.1.min"],
        "jquery.validate" : ["lib/jquery.validate"],
        "additional-methods" : ["lib/additional-methods"],
        "yzm" : ["lib/gVerify"],
        "storageTools" : ["storageTools"],
        "jquery.cookie":["lib/jquery.cookie"],
        shim : {
            "yzm" : {
                deps : ["jquery"] // deps 对于当前的非模块的js文件是否依赖其他模块
            }
        }
    }
});
requirejs(["jquery","jquery.validate", "storageTools","jquery.cookie","yzm"], function($, tools){
    $(function () {
        var verifyCode = new GVerify("v_container");
        $("button").click(function () {

            if ($("input[name=yanzhenma]").val().length>1){
                var res = verifyCode.validate($("input[name=yanzhenma]").val())
                console.log($("input[name=yanzhenma]").val());
                if(res){
                    $(".error_yzm").hide();
                    flag=true;
                }else{
                    $(".error_yzm").show();
                    flag = false;
                }
            }

        })
        $("form").validate({
            rules:{
                uname:{
                    required:true,
                },
                upwd:{
                    required:true,
                },
                yanzhenma:{
                    required:true,
                }
            },
            messages:{
                uname:{
                    required:"请输入用户名"
                },
                upwd:{
                    required:"请输入密码"
                },
                yanzhenma:{
                    required:"请输入验证码"

                }
            },
            submitHandler:function () {
                if (flag){
                    $.ajax({
                        url:"http://10.41.155.29:8080/mecoxlanenet/server/login.php",
                        type:"POST",
                        dataType:"json",
                        data:{
                            uname:$("input[name=uname]").val(),
                            upwd:$("input[name=upwd]").val()
                        }
                    }).then(function (res) {
                        if (res.status==1){
                            sessionStorage.setItem("login",JSON.stringify(res.data));
                            var ajaxList = [];
                            const cookieData = JSON.parse($.cookie("cartInfo") || '[]');

                            cookieData.forEach((el, index) =>{
                                el.uId=res.data.uid;//登录成功后,得到当前登陆者id
                                console.log(el);
                            ajaxList.push(
                                $.ajax({
                                    url : "http://10.41.155.29:8080/mecoxlanenet/server/optCart.php",
                                    data:el, //解决这个数据就解决了 登录问题
                                    type : "post"
                                })
                            );
                        });
                            Promise.all(ajaxList).then(function(){
                                $.cookie("cartInfo", "", {expires : -1000});
                                alert("登录成功");
                                location = "index.html";
                            })
                        }else{
                            alert(res.msg);

                        }
                    })
                }else {
                    alert("登录失败");
                }
                return false;
            }
        })
    })
})
